package com.example.twoscroolview.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.animation.PathInterpolator
import androidx.appcompat.widget.AppCompatTextView

class SimpleColorChangeTextView : AppCompatTextView {


    //渐变的进度
    var percent = 0f
        set(value) {
            System.out.println("====$percent")
            field = value
            invalidate()
        }

    constructor(context: Context) : super(context) {
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        drawCenterText(canvas)
        drawCenterText1(canvas)
        //  drawBaseLine(canvas)
        //   drawHorCenterLine(canvas)
    }

    fun drawCenterText(canvas: Canvas?) {
        val paint2 = Paint()
        val text = "想学课堂"
        paint2.textSize = 80f
        paint2.textAlign = Paint.Align.LEFT
        val textWidth = paint2.measureText(text)
        val left = width / 2 - textWidth / 2
        val mFontMetrics = paint2.fontMetrics
        val baseline = height / 2f - (mFontMetrics.descent.plus(mFontMetrics.ascent)) / 2
        canvas?.drawText(text, left, baseline, paint2)
    }

    fun drawCenterText1(canvas: Canvas?) {
        canvas?.save()
        val mPaint = Paint()
        val text = "想学课堂"
        mPaint.textSize = 80f
        mPaint.textAlign = Paint.Align.LEFT
        mPaint.color = Color.RED
        val textWidth = mPaint.measureText(text)
        val left = width / 2 - textWidth / 2
        val right = left + textWidth * percent
        val clipReact = Rect(left.toInt(), 0, right.toInt(), height)
        val mFontMetrics = mPaint.fontMetrics
        val baseline = height / 2f - (mFontMetrics.descent.plus(mFontMetrics.ascent)) / 2
        canvas?.clipRect(clipReact)
        canvas?.drawText(text, left, baseline, mPaint)
        canvas?.restore()
    }

    fun drawBaseLine(canvas: Canvas?) {
        val mPaint2 = Paint()
        mPaint2.strokeWidth = 3f
        mPaint2.color = Color.RED
        mPaint2.style = Paint.Style.FILL
        canvas?.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), mPaint2)
    }

    fun drawHorCenterLine(canvas: Canvas?) {
        val mPaint2 = Paint()
        mPaint2.strokeWidth = 3f
        mPaint2.color = Color.RED
        mPaint2.style = Paint.Style.FILL
        canvas?.drawLine(0f, height / 2f, width.toFloat(), height / 2f, mPaint2)
    }
}